package com.wooboo.server.ext.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Random;


public class StringUtil {
	private static final String BLANK = "";
	public static void main(String[] args){
		
		String str = "f5f6a26f-c7cc-4299-a414-6a8e9483f823";
		System.out.println(str.replaceAll("-", "").toLowerCase());
		
		Double b= 20000d;
		System.out.println(b.intValue());
		
		String reg= "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		System.out.println("fujia_n  yd@163.@com".matches(reg));
		System.out.println("127.0.2.1".matches("127.0.2.[1-2]"));
		System.out.println("127.0.2.1".matches(null));
		/***
		 * 
		 * 
		 * appid=guomeiplus&source=shike&idfa=F5F6A26F-C7CC-4299-A414-6A8E9483E923
		 * 
		StringBuffer sbuf = new StringBuffer("com.pet.server.ext.");
		String uri = "/huaext/rest/2.0/product/list";
		uri = uri.replaceAll(".*\/rest/2.0/", "");
		String[] parts = uri.split("\\/");
		sbuf.append(parts[0]+".");
		sbuf.append(Character.toUpperCase(parts[1].charAt(0)));
		sbuf.append(parts[1].substring(1));
		System.out.println(sbuf.toString());
		System.out.println("/huaext/rest/2.0/product/list".matches(".*\/rest/2.0/.*"));

		*/
	}
	
	
	
	public static boolean isEmpty(String s){
		return s == null || BLANK.compareTo(s) == 0;
	}
	public static boolean isEmpty(Object str) {
		return str == null || BLANK.equals(str);
	}
	public static boolean isEmpty(Object ...strs){
		for(Object str:strs){
			if(isEmpty(str)){
				return true;
			}
		}
		return false;
	}
	public static boolean allEmpty(Object ...strs){
		for(Object str:strs){
			if(!isEmpty(str)){
				return false;
			}
		}
		return true;
	}
	public static String dealNull(String str) {
		return str != null ? str.trim() : BLANK;
	}
	public static String dealNull(String str, String defaultVal) {
		return str != null ? str.trim() : defaultVal;
	}
	public static String dealEmpty(String str, String defaultVal) {
		return !isEmpty(str) ? str.trim() : defaultVal;
	}
	public static String dealNull(Object str,String defaultValue){
		return str != null ? str.toString().trim() : defaultValue;
	}
	public static String dealNull(Object obj) {
		String str = BLANK;
		if(obj!=null){
			if(obj instanceof String){
				str = (String)obj;
			}else{
				str = obj.toString();
			}
		}
		return str;
	}
	
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');
			if ((i > -1) && (i < (filename.length() - 1))) {
				return filename.substring(i + 1);
			}
		}
		return null;
	}
	public static String getFileName(String filepath){
		if ((filepath != null) && (filepath.length() > 0)) {
			int i = filepath.lastIndexOf('/');
			if ((i > -1) && (i < (filepath.length() - 1))) {
				return filepath.substring(i + 1);
			}
		}
		return filepath;
	}
	public static String getFilePath(String filepath){
		if ((filepath != null) && (filepath.length() > 0)) {
			int i = filepath.lastIndexOf('/');
			if ((i > -1) && (i < (filepath.length() - 1))) {
				return filepath.substring(0,i + 1);
			}
		}
		return filepath;
	}
	public static boolean equals(Object value1, Object value2) {
		boolean is = false;
		if (value1 == value2) { // is null or self
			return is = true;
		}
		if (value1 != null && value2 != null) { // is not null;
			return value1.equals(value2);
		}
		return is;
	}
	public static int random(int n){
		if(n==0) return 0;
		Random random = new Random();
		return Math.abs(random.nextInt())%n;
	}
	public static String formatOrderId(Date date,Integer orderId){
        String prefix = DateUtil.getFormatDate(date, "yyyyMM");// createdTime.substring(0, tIndex).replace("-", "").substring(0, 6);        
        return String.format("%s%06d", prefix, orderId);
    }
	public static String createUuid(String mac,String imei){
		return MD5UtilBak.MD5Encode(mac.replaceAll(":","")+imei, null);
	}
	public static String createId(Integer length) { 
		String base = "0123456789";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 } 
	
	public static String encode(String str, final String encoding) {
		try {
			if (!isEmpty(str)) {
				str = URLEncoder.encode(str, encoding);
			}
		} catch (Exception e) {
		}
		return str;
	}
	public static String decode(String str, final String encoding) {
		try {
			if (!isEmpty(str)) {
				str = URLDecoder.decode(str, encoding);
			}
		} catch (Exception e) {
		}
		return str;
	}
}